import { RootState } from '@/types/store'
import { useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'

// 继承每一项
const useInitState = <StateName extends keyof RootState> (action : ()=>void, stateName : StateName) => {
  const dispatch = useDispatch()
  useEffect(() => {
    dispatch(action())
  }, [dispatch])
  return useSelector((state:RootState) => state[stateName])
}

export default useInitState
